package com.zFace.LRU;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Practice<K, V> extends LinkedHashMap<K, V> {

	private final int maxCapacity;

	private static final float defaultLoadFactory = 0.75f;

	public Practice(int maxCapacity) {
		super(maxCapacity, defaultLoadFactory, true);
		this.maxCapacity = maxCapacity;
	}

	private final Lock lock = new ReentrantLock();


	@Override
	public boolean removeEldestEntry(Map.Entry<K, V> eldest) {
		return size() > maxCapacity;
	}

	@Override
	public boolean containsKey(Object key) {
		lock.lock();
		try {
			return super.containsKey(key);
		} finally {
			lock.unlock();
		}
	}


	public static void main(String[] args) {
		Map map = new Practice(3);
		map.put(1, "a");
		System.out.println(map);
		map.put(2, "b");
		System.out.println(map);
		map.put(3, "c");
		System.out.println(map);
		map.put(4, "d");
		System.out.println(map);
		map.put(1, "a");
		System.out.println(map);
		map.put(2, "b");
		System.out.println(map);
	}
}
